package tests.cases {
	
	import mx.rpc.AsyncToken;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	import org.flexunit.Assert;
	
	import tests.cases.DefaultTestCase;
	
	public class TestAdminGroups extends DefaultTestCase {
		
		private var administratorService:RemoteObject;
		
		[Before]
		public function setUp():void {
			
			administratorService = new RemoteObject();
			administratorService.destination = "amfphp";
			administratorService.source = "AdministratorService";
		}
		
		[After]
		public function tearDown():void {
			
			administratorService = null;
		}
		
		// ----------------------------------------------------------------
		// TEST GROUP LIST
		// ----------------------------------------------------------------
		[Test(async,order=1)]
		public function testGetGroupList():void {
			
			var argsVO:Object = {
				sessionID: "debug"
			};
			
			var token:AsyncToken = administratorService.getGroupList(argsVO.sessionID);
			runAsyncTest(token, argsVO, handleGetGroupList);
		}
		
		private function handleGetGroupList(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			Assert.assertTrue((message.result.length > 0));
		}
		
		// ----------------------------------------------------------------
		// TEST SMS GROUP
		// ----------------------------------------------------------------
		[Test(async,order=2)]
		public function testSMSGroup():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				groupUID: "group001",
				apiID: "test",
				userName: "test",
				password: "test",
				message: "This is a test message"
			};
			
			var token:AsyncToken = administratorService.smsGroup(argsVO.sessionID, argsVO.groupUID, argsVO.apiID, argsVO.userName, argsVO.password, argsVO.message);
			runAsyncTest(token, argsVO, handleSMSGroup);
		}
		
		private function handleSMSGroup(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNull(message.result);
		}
		
		// ----------------------------------------------------------------
		// TEST EMAIL GROUP
		// ----------------------------------------------------------------
		[Test(async,order=3)]
		public function testEmailGroup():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				groupUID: "group001",
				fromAddress: "test@test.com",
				heading: "Test Message",
				message: "This is a test message"
			};
			
			var token:AsyncToken = administratorService.emailGroup(argsVO.sessionID, argsVO.groupUID, argsVO.fromAddress, argsVO.heading, argsVO.message);
			runAsyncTest(token, argsVO, handleEmailGroup);
		}
		
		private function handleEmailGroup(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNull(message.result);
		}
		
		// ----------------------------------------------------------------
		// TEST ADD GROUP
		// ----------------------------------------------------------------
		private static var addedGroupUID:String;
		
		[Test(async,order=4)]
		public function testAddGroup():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				userUID: "user001",
				groupName: "Test Group",
				description: "This is another test group",
				logoURL: "assets/flash.png",
				chatURL: "",
				sponsors: [{sponsorName:"XYZ Plumbers", description:"Sponsoring all of our plumbing needs!", sponsorURL:"www.google.com"}],
				modules: ["mod0", "mod1", "mod2", "mod3"]
			};
				
			var token:AsyncToken = administratorService.addGroup(
				argsVO.sessionID, argsVO.userUID,
				argsVO.groupName, argsVO.description,
				argsVO.logoURL, argsVO.chatURL,
				argsVO.sponsors, argsVO.modules
			);
			runAsyncTest(token, argsVO, handleAddGroup);
		}
		
		private function handleAddGroup(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			addedGroupUID = message.result as String;
		}
		
		// ----------------------------------------------------------------
		// TEST EDIT GROUP
		// ----------------------------------------------------------------
		[Test(async,order=5)]
		public function testEditGroup():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				groupUID: "group001",
				groupName: "Test Group 2",
				description: "This is a test group",
				logoURL: "assets/flash.png",
				chatURL: "http://connectnow.acrobat.com/fdempers/testroom",
				sponsors: [],
				modules: ["mod0", "mod1", "mod2", "mod3"]
			};
				
			var token:AsyncToken = administratorService.editGroup(
				argsVO.sessionID, argsVO.groupUID,
				argsVO.groupName, argsVO.description,
				argsVO.logoURL, argsVO.chatURL,
				argsVO.sponsors, argsVO.modules
			);
			runAsyncTest(token, argsVO, handleEditGroup);
		}
		
		private function handleEditGroup(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			Assert.assertEquals("group001", message.result);
		}
		
		// ----------------------------------------------------------------
		// TEST DELETE GROUP
		// ----------------------------------------------------------------
		[Test(async,order=6)]
		public function testDeleteGroup():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				groupUID: addedGroupUID
			};
			
			var token:AsyncToken = administratorService.deleteGroup(argsVO.sessionID, argsVO.groupUID);
			runAsyncTest(token, argsVO, handleDeleteGroup);
		}
		
		private function handleDeleteGroup(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			Assert.assertEquals(addedGroupUID, message.result);
		}
		
		// ----------------------------------------------------------------
		// TEST GET MODULE LIST
		// ----------------------------------------------------------------
		[Test(async,order=7)]
		public function testGetModuleList():void {
			
			var argsVO:Object = {
				sessionID: "debug"
			};
			
			var token:AsyncToken = administratorService.getModuleList(argsVO.sessionID);
			runAsyncTest(token, argsVO, handleGetModuleList);
		}
		
		private function handleGetModuleList(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			Assert.assertTrue((message.result.length > 0));
		}
		
		// ----------------------------------------------------------------
		// TEST GET GROUP DETAILS
		// ----------------------------------------------------------------
		[Test(async,order=8)]
		public function testGetGroupDetails():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				groupUID: "group001"
			};
			
			var token:AsyncToken = administratorService.getGroupDetails(argsVO.sessionID, argsVO.groupUID);
			runAsyncTest(token, argsVO, handleGetGroupDetails);
		}
		
		private function handleGetGroupDetails(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			
			Assert.assertNotNull(message.result.modules);
			Assert.assertNotNull(message.result.sponsors);
		}
		
		// ----------------------------------------------------------------
		// TEST ACTIVATE CHAT ROOM
		// ----------------------------------------------------------------
		[Test(async,order=9)]
		public function testActivateChatRoom():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				groupUID: "group001",
				chatURL: "http://connectnow.acrobat.com/fdempers/testroom"
			};
			
			var token:AsyncToken = administratorService.activateChatRoom(argsVO.sessionID, argsVO.groupUID, argsVO.chatURL);
			runAsyncTest(token, argsVO, handleActivateChatRoom);
		}
		
		private function handleActivateChatRoom(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNull(message.result);
		}
	}
}